<?php
$JO[ "Fashion" ] 		= JO(array(
		"EN"=>"JukeOn™ Fashion",
		"VI"=>"Thời Trang JukeOn™"
	) );
$JO["JO_searchid"] 	 	= JO(array("EN"=>"Search Key",			"VI"=>"Search"	));

if(!isset ($_SESSION["JO_account_key"]) || $_SESSION["JO_account_authority"]!="SO"){

	echo "<script>location='http://jukeon.vn/JO/';</script>";
}
$searchkeyartist=$JO["JO_searchid"];
$wherequery="";
if(isset ($_REQUEST["searchkey"])){
    $searchkeyartist=$_REQUEST["searchkey"];
    if($searchkeyartist!=$JO["JO_searchid"]){
        $wherequery= "and artist.`name` like '%".$searchkeyartist."%'";
    }
}
$page=1;
if(isset ($_REQUEST["Page"])){$page=$_REQUEST["Page"];}
?>
<div class="JO_page">

	<h1><?= $JO["Fashion"] ?></h1>
        <h2>&nbsp;</h2>
        <h3>&nbsp;</h3>
      

</div>
<script type="text/javascript">
    var musics=new Array();
    var n=0;
</script>
<div class="JO_relative" style="width: 960px">
     <div class="JO_left" style="width: 240px;">
        <div class="JO_box_240">
            <h1>Artist</h1>
            <input
            id="txtsearchartist"
            type="text"
            style="width: 212px"
            value="<?= urldecode($searchkeyartist)?>"
            onfocus="searchfocus(this);"
            onblur="searchblur(this);"
            onkeypress="searchKeyPress(event);"/>
         <table style="width: 100%">
             <?
             $querycount="
			 SELECT
                                artist.`name`,
                                artist.`key`,
                                max(music.position) as top
                            FROM
                                artist
                                INNER JOIN music ON artist.`key` = music.artist
                            WHERE music.`delete` is NULL and artist.`delete` is null ".$wherequery."
                            GROUP BY artist.`name`
                            ORDER BY artist.`name`
			 ";
             $recordSetnum = JO_query( $querycount );
             $numpages=$recordSetnum->num_rows;
             if(($numpages%30)!=0) {$numpages=(int)($numpages/30)+1;}
             else                {$numpages=$numpages/30;}
            $query="
            SELECT
                artist.`name`,
                artist.`key`,
                max(music.position) as top
            FROM
                artist
                INNER JOIN music ON artist.`key` = music.artist
            WHERE music.`delete` is NULL and artist.`delete` is null ".$wherequery."
            GROUP BY artist.`name`
            ORDER BY artist.`name`
			Limit ".(($page-1)*30).",30
            ";
             //$query="Select * from `artist` where `delete` is null and `name` is not null ".$wherequery." group by `name` order by `name` Limit ".(($page-1)*30).",30";
             $recordSet = JO_query( $query );
             $showselect=TRUE;
             if($recordSet->num_rows) while ($record = $recordSet -> fetch_array( )){
             ?>
             <tr <? if(isset ($_REQUEST["artistid"]) && $_REQUEST["artistid"]==$record["name"]){echo "style='background-color: #FDF7D9'"; }elseif($showselect && !isset ($_REQUEST["artistid"])){echo "style='background-color: #FDF7D9'";$showselect=false;}else{?>onclick="sendurl2('<?=urlencode($record["name"])?>');"<?}?>
                  style="cursor: pointer">
                 <td <? if(isset ($record["top"])){echo 'class="checktop"'; }?> ><?=$record["name"]?></td>
             </tr>
             <?
             }
             ?>
             <tr ><td style="text-align: center;border-bottom: 0px"><?

                if($numpages>1){
                    if($page>2){
                    ?>
                        <a href="#" onclick="sendurl('1');">◀</a>
                    <?
                    }
                    if($page>1){
                    ?>
                        <a href="#" onclick="sendurl('<?=($page-1)?>');">◁</a>

                    <?
                    }
                    if($page-4>1) echo "...";
                    for($i=$page-4;$i<=($page-1);$i++){
                        if($i>0){
                        ?>
                        <a href="#" onclick="sendurl('<?=$i?>');"><?=$i?></a>
                        <?
                        }
                    }
                    echo  "<b style='color=#ffffff;'>".$page."</b>";
                    for($i=$page+1;$i<=($page+4);$i++){
                        if($i<=$numpages){
                        ?>
                        <a href="#" onclick="sendurl('<?=$i?>');"><?=$i?></a>
                        <?
                        }
                    }
                    if($page+4<$numpages) echo "...";
                    if($page<$numpages){
                    ?>
                        <a href="#" onclick="sendurl('<?=($page+1) ?>');">▷</a>
                    <?
                    }
                    if($page<($numpages-1)){
                    ?>
                        <a href="#" onclick="sendurl('<?=$numpages ?>');">▶</a>

                    <?
                    }

                }
                 ?></td></tr>
             
         </table>
        </div>
     </div>
    <div class="JO_left">
    <div class="JO_bar_720">
            <?
            if(isset ($_REQUEST["artistid"])){
                $query="Select * from `artist` where `delete` is null and `name` is not null and `name`='".$_REQUEST["artistid"]."'";
            }else{
                $query="Select * from `artist` where `delete` is null and `name` is not null ".$wherequery." group by `name` order by `name` Limit ".(($page-1)*30).",1";
            }
                $recordSet = JO_query( $query );
                if($recordSet->num_rows) $record = $recordSet -> fetch_array( )
            ?>

        <h1>Top Music Of Artist<? echo "-".$record["name"];$artistid=$record["name"];?></h1>
<!--        <input
            id="txtsearchartist"
            type="text"
            style="width: 692px"
            value="<?//=$searchkeyartist?>"
            onfocus="searchfocus(this);"
            onblur="searchblur(this);"
            onkeypress="searchKeyPress(event);"/>-->
<!--        <select id="cbxartist" style="width: 100%" onchange="changeartist(this);">
            <?
                //$query="Select * from `artist` where `delete` is null and `name` is not null ".$wherequery." group by `name` order by `name` Limit ".(($page-1)*30).",30";
                //$recordSet = JO_query( $query );
                //if($recordSet->num_rows) while ($record = $recordSet -> fetch_array( )){
            ?>
            <option value="<?//=$record["name"]?>" <? //if($record["name"]==$_REQUEST["artistid"]){ echo 'selected=1/';}?>><?//=$record["name"]?></option>
            <?
                //}
            ?>
        </select>-->













        <span id="tb_top_music">
            <table style="width: 100%;margin: 0px 4px 0px 0px;">
                <tr>
                    <th style="width: 50px">Top</th>
                    <th>Top Music</th>
                    <th style="width: 20PX"></th>
                    <th style="width: 20PX"></th>
                    <th style="width: 20PX"></th>
                </tr>
            </table>
                <?
                /*if(isset ($_REQUEST["artistid"])){
                    $query="Select music.*
                            from `music`,artist
                            where music.artist=artist.`key`
                                and music.`delete` is null
                                and artist.`delete` is null
                                and `artist`.`name`='".$_REQUEST["artistid"]."'
                                and position is not null order by `position` desc";
                }else{
                    $query="Select music.*,artist.`name`
                            from `music`,artist
                            where music.artist=artist.`key`
                                and music.`delete` is null
								and artist.`delete` is null
								and not isnull(position)
                                and artist.`name`=
                                    (Select `name` from `artist` where `delete` is null and `name` is not null ".$wherequery." group by `name` order by `name` Limit ".(($page-1)*30).",1)
								and position is not null order by `position` desc	
									";
                }*/
				if(isset ($_REQUEST["artistid"])){
                    $query="
							SELECT  if(COUNT( rbt.`key` )>0,1,0) AS sr,COUNT( rbt.`key` ),music . * 
							FROM  (artist,`music`) left join rbt on (rbt.music = music.`key`)
							WHERE music.artist = artist.`key` 
								
								AND music.`delete` IS NULL 
								AND artist.`delete` IS NULL 
								AND  `artist`.`name` =  '".$_REQUEST["artistid"]."'
								AND position IS NOT NULL 
							group by music.`key`
							having sr=1
							ORDER BY  sr desc,`subject` 
							
                                ";
                }else{
                    $query="SELECT  if(COUNT( rbt.`key` )>0,1,0) AS sr,COUNT( rbt.`key` ),music . * 
							FROM  (artist,`music`) left join rbt on (rbt.music = music.`key`)
							WHERE music.artist = artist.`key` 
								
								AND music.`delete` IS NULL 
								AND artist.`delete` IS NULL 
								AND  `artist`.`name` =  (Select `name` from `artist` where `delete` is null and `name` is not null ".$wherequery." group by `name` order by `name` Limit ".(($page-1)*30).",1)
								AND position IS NOT NULL 
							group by music.`key`
							having sr=1
							ORDER BY  sr desc,`subject` 
								";
                }
                  $recordSet = JO_query( $query );
				  $ntop=1;
                if($recordSet->num_rows) while ($record = $recordSet -> fetch_array( )){
					
					//if(!isset ($_REQUEST["artistid"])){$artistid=$record["name"];}
                     ?>
            <span id='trtop_<?=$record["key"]?>'>
                     <table  style="width: 100%;margin: 0px 4px 0px 0px;">
                        <tr >
                        <td style="width: 50px" id='tdtopid_<?=$record["key"]?>'><?=$ntop?></td>
                        <td id='tdtop_<?=$record["key"]?>'><?=$record["subject"]?></td>
                        <td style="width: 12px"><a href='#i' onclick='topup("<?=$record["key"]?>");'><img src="JO_80_image/JO_27_icon_up_on.png" class="JO_icon" alt="Up" title="Up"/></a></td>
                        <td style="width: 12px"><a href='#i' onclick='topdown("<?=$record["key"]?>");'><img src="JO_80_image/JO_28_icon_down_on.png" class="JO_icon" alt="Down" title="Down"/></a></td>
                        <td style="width: 12px"><a href='#i' onclick='removetop("<?=$record["key"]?>");'><img src="JO_80_image/JO_23_icon_delete_on.png" class="JO_icon" alt="Remove" title="Remove"/></a></td>
                        </tr>
                     </table>
                </span>
                         <script type="text/javascript">
                            musics[n]=<?=$record["key"]?>;
                            n++;
                        </script>
                     <?
					 $ntop++;
                 }
                 
                ?>
        
        </span>
        
        <input type="button" class="JO_button" style="width: 100%" value="Save" onclick="save();"/>
            <?
            //if(isset($_REQUEST["artistid"])){
                ?>
        <span id="tb_music" >
            <table style="width: 100%;margin: 0px 4px 0px 0px;">
                <tr>
                    <th>Subject</th>
                    <th style="width: 20px"></th>
                </tr>
            </table>
                <?
                /*if(isset ($_REQUEST["artistid"])){
                    $query="
							
							Select music.* 
                            from `music`,artist
                            where music.artist=artist.`key`
                                and music.`delete` is null
                                and artist.`delete` is null
                                and `artist`.`name`='".$_REQUEST["artistid"]."'
                                and position is null order by `subject`
                                ";
                }else{
                    $query="Select music.*,artist.`name`
                            from `music`,artist
                            where music.artist=artist.`key`
                                and music.`delete` is null
								and artist.`delete` is null
                                and artist.`name`=
                                    (Select `name` from `artist` where `delete` is null and `name` is not null ".$wherequery." group by `name` order by `name` Limit ".(($page-1)*30).",1)
                                and position is null order by `subject`
                                ";
                }*/
                if(isset ($_REQUEST["artistid"])){
                    $query="
							SELECT  if(COUNT( rbt.`key` )>0,1,0) AS sr,COUNT( rbt.`key` ),music . * 
							FROM  (artist,`music`) left join rbt on (rbt.music = music.`key`)
							WHERE music.artist = artist.`key` 
								
								AND music.`delete` IS NULL 
								AND artist.`delete` IS NULL 
								AND  `artist`.`name` =  '".$_REQUEST["artistid"]."'
								AND position IS  NULL 
							group by music.`key`
							having sr=1
							ORDER BY  sr desc,`subject` 
							
                                ";
                }else{
                    $query="
							SELECT  if(COUNT( rbt.`key` )>0,1,0) AS sr,COUNT( rbt.`key` ),music . * 
							FROM  (artist,`music`) left join rbt on (rbt.music = music.`key`)
							WHERE music.artist = artist.`key` 
								
								AND music.`delete` IS NULL 
								AND artist.`delete` IS NULL 
								AND  `artist`.`name` =  (Select `name` from `artist` where `delete` is null and `name` is not null ".$wherequery." group by `name` order by `name` Limit ".(($page-1)*30).",1)
								AND position IS  NULL 
							group by music.`key`
							having sr=1
							ORDER BY  sr desc,`subject` 
					
                                ";
                }
                $recordSet = JO_query( $query );
                if($recordSet->num_rows) while ($record = $recordSet -> fetch_array( )){
					//if(!isset ($_REQUEST["artistid"])){$artistid=$record["name"];}
                    ?>
            <span id="tr_<?=$record["key"]?>">
                <table style="width: 100%;margin: 0px 4px 0px 0px;">
                <tr >
                    <td id="td_<?=$record["key"]?>"><?=$record["subject"]?></td>
                    <td style="width: 12px"><a href="#i" onclick='addtop("<?=$record["key"]?>");'><img src="JO_80_image/add_top.png" class="JO_icon" alt="Add To Top Music" title="Add To Top Music"/></a></td>
                </tr>
                </table>
                </span>
                    <?
                }
                ?>
            </span>
                <?
            //}
            ?>
        <div id ="kqkq"></div>
        
    </div>
    </div>
 <script type="text/javascript">
    function addtop(id){
        alert(id);
        var contenttop="<span id='trtop_"+id+"'>";
        contenttop+="<table  style='width: 100%;margin: 0px 4px 0px 0px;'>";
        contenttop+="<tr >";
        contenttop+="<td style='width: 50px' id='tdtopid_"+id+"'>"+id+"</td>";
        contenttop+="<td id='tdtop_"+id+"'>"+document.getElementById("td_"+id).innerHTML+"</td>";
        contenttop+="<td style='width: 12px'><a href='#i' onclick='topup("+id+");'><img src='JO_80_image/JO_27_icon_up_on.png' class='JO_icon' alt='Up' title='Up'/></a></td>";
        contenttop+="<td style='width: 12px'><a href='#i' onclick='topdown("+id+");'><img src='JO_80_image/JO_28_icon_down_on.png' class='JO_icon' alt='Down' title='Down'/></a></td>";
        contenttop+="<td style='width: 12px'><a href='#i' onclick='removetop("+id+");'><img src='JO_80_image/JO_23_icon_delete_on.png' class='JO_icon' alt='Remove' title='Remove'/></a></td>";
        contenttop+="</tr>";
        contenttop+="</table>";
        contenttop+="</span>";
        document.getElementById("tb_top_music").innerHTML+=contenttop;

        //alert(document.getElementById("tb_top_music").innerHTML);
        var del = document.getElementById("tr_"+id);
        del.parentNode.removeChild(del);
        musics[n]=id;
        n++;
        //document.getElementById("kqkq").innerHTML=musics;
        create_top();
    }
    function removetop(id){
        
        var contentmusic="<span id='tr_"+id+"'>";
        contentmusic+="<table style='width: 100%;margin: 0px 4px 0px 0px;' >";
        contentmusic+="<tr ><td id='td_"+id+"'>"+document.getElementById("tdtop_"+id).innerHTML+"</td>";
        contentmusic+="<td style='width:12px'><a href='#i' onclick='addtop("+id+");'><img src='JO_80_image/add_top.png' class='JO_icon' alt='Add To Top Music' title='Add To Top Music'/></a></td>";
        contentmusic+="</tr>";
        contentmusic+="</table>";
        contentmusic+="</span>";
        document.getElementById("tb_music").innerHTML+=contentmusic;
        var del=document.getElementById("trtop_"+id);
        del.parentNode.removeChild(del);
        for(var i=0;i<musics.length;i++){
            if(musics[i]==id){
                musics[i]=undefined;
                create_top();
                break;
            }
        }
        
    }
    function topup(id){

        //alert(document.getElementById("trtop_"+id).id+":id");
        for(var i=0;i<musics.length;i++){
            if(musics[i]==id && i>0){
                var j=1;
                while(musics[i-j]==undefined && i-j>=0){
                    j++;
                }

                var contenttmp=document.getElementById("trtop_"+musics[i]).innerHTML;
//                var contenttmp2=document.getElementById("trtop_"+musics[i-j]);
//                var pct=contenttmp.parentNode;
//                pct.replaceChild(contenttmp,contenttmp2);
//                pct.replaceChild(contenttmp2,contenttmp);
                document.getElementById("trtop_"+musics[i]).innerHTML=document.getElementById("trtop_"+musics[i-j]).innerHTML;;
                document.getElementById("trtop_"+musics[i-j]).innerHTML=contenttmp;

                var idtmptr=document.getElementById("trtop_"+musics[i]).id;
                var idtmptr2=document.getElementById("trtop_"+musics[i-j]).id;
                document.getElementById("trtop_"+musics[i]).id=document.getElementById("trtop_"+musics[i]).id+"abc123";
                document.getElementById("trtop_"+musics[i-j]).id=idtmptr;
                document.getElementById("trtop_"+musics[i]+"abc123").id=idtmptr2

                //hoan vi 2 element
                var tmp=musics[i];
                musics[i]=musics[i-j];
                musics[i-j]=tmp;

                //document.getElementById("kqkq").innerHTML=musics;
                create_top();
                //alert(musics);
                break;
            }
        }
        
    }
    function topdown(id){
	
        //alert(document.getElementById("trtop_"+id).id+":id");
        for(var i=0;i<musics.length;i++){
            if(musics[i]==id){
                var j=1;
                while(musics[i+j]==undefined && i+j<musics.length){

                    j++;
                }
                if(musics[i+j]==undefined){ return;}
                //alert(musics[i]+":"+musics[i+j]);
               
 

                var contenttmpdown=document.getElementById("trtop_"+musics[i]).innerHTML;
                document.getElementById("trtop_"+musics[i]).innerHTML=document.getElementById("trtop_"+musics[i+j]).innerHTML;
                document.getElementById("trtop_"+musics[i+j]).innerHTML=contenttmpdown;
                //alert(document.getElementById("trtop_"+musics[i+j]).id);

                var idtmptr=document.getElementById("trtop_"+musics[i]).id;
                var idtmptr2=document.getElementById("trtop_"+musics[i+j]).id;
                document.getElementById("trtop_"+musics[i]).id=document.getElementById("trtop_"+musics[i]).id+"abc123";
                document.getElementById("trtop_"+musics[i+j]).id=idtmptr;
                document.getElementById("trtop_"+musics[i]+"abc123").id=idtmptr2;
                
                //hoan vi 2 element
                var tmp=musics[i];
                musics[i]=musics[i+j];
                musics[i+j]=tmp;

                //document.getElementById("kqkq").innerHTML=musics;
                create_top();
                //alert(musics);
                break;
            }
        }

    }
    function save(){
        
        var top=";";
        for(var i=0;i<musics.length;i++){
            if(musics[i]!=undefined){
                top+=musics[i]+";";
            }
        }
        //if(top==";"){return;}
		//alert(top);
        //
		//document.getElementById( "JO_integration_asynchronous" ).src = "JO_05_execution.php?JO_query=SaveTopArtist&top="+top+"&artist=<?=$artistid?>";
		location = "JO_05_execution.php?JO_query=SaveTopArtist&top="+top+"&artist=<?=$artistid?>";
    }
    function create_top()
    {
        var ntop=1;
        for(var i=0;i<musics.length;i++){
            if(musics[i]!=undefined){
                document.getElementById("tdtopid_"+musics[i]).innerHTML=ntop;
                ntop++;
            }
        }
    }
    function changeartist(c){
        location="JO_00_integration.php?JO_unit=new_topartist&artistid="+c.value+"&searchkey=<?=urlencode($searchkeyartist)?>";
    }
    function searchfocus(v){
        if(v.value=="<?=$JO["JO_searchid"]?>")
            v.value="";
    }
    function searchblur(v){
        var s=v.value;
        //Cắt bỏ những khoảng trắng đầu chuỗi
        while(s.charCodeAt(0)==32)
            s=s.substring(1);
        //Cắt bỏ những khoảng trắng cuối chuỗi
        while(s.charCodeAt(s.length-1)==32)
            s=s.substring(0,s.length-1);
        if(s=="")
            v.value="<?=$JO["JO_searchid"]?>";
        else
            v.value=s;
    }
    window.onload = function() {
        document.getElementById("txtsearchartist").focus();
    }
    function searchKeyPress(e)
    {
        // look for window.event in case event isn't passed in
        if (window.event) { e = window.event; }
        if (e.keyCode == 13)
        {
            key=document.getElementById("txtsearchartist").value;
            if(key=="<?=$JO["JO_searchid"]?>" || key==""){

                location=("JO_00_integration.php?JO_unit=new_topartist");
            }else{

                location=("JO_00_integration.php?JO_unit=new_topartist&searchkey="+document.getElementById("txtsearchartist").value);
            }

        }
    }
    function sendurl(p){
            <?
            if($searchkeyartist==$JO["JO_searchid"]){
              ?>
                location=("JO_00_integration.php?JO_unit=new_topartist&Page="+p);
                <?
            }else{
                ?>
                location=("JO_00_integration.php?JO_unit=new_topartist&Page="+p+"&searchkey=<?=urlencode($searchkeyartist)?>");
                <?
            }
            ?>
            
    }
    function sendurl2(p){
            <?
            if($searchkeyartist==$JO["JO_searchid"]){
              ?>
                location=("JO_00_integration.php?JO_unit=new_topartist&Page=<?=$page?>&artistid="+p);
                <?
            }else{
                ?>
                        location=("JO_00_integration.php?JO_unit=new_topartist&Page=<?=$page?>&artistid="+p+"&searchkey=<?=  urlencode($searchkeyartist)?>");
                <?
            }
            ?>

    }
 </script>
</div>